package com.naizhuai.ui.user.simulation;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.exception.ValidateException;
import com.naizhuai.ui.components.CustomTextWatch;
import com.naizhuai.ui.entity.User.DeviceData;
import com.naizhuai.ui.entity.User.Simulation;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author：lichenxi
 * @date 2018/7/3 19
 * email：525603977@qq.com
 * Fighting
 */
public class DeviceDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private AddAndDeleteCallBack mAndDeleteCallBack;
   private List<DeviceData> mDeviceDataList;
    private  boolean editenable=true;

    public void setEditenable(boolean editenable) {
        this.editenable = editenable;
    }

    public List<DeviceData> getResult() throws ValidateException {
        if (mDeviceDataList==null){
            throw new ValidateException("数据为空");
        }
        for (DeviceData deviceData : mDeviceDataList) {
            if (TextUtils.isEmpty(deviceData.machineName)){
                throw new ValidateException("设备名称尚未填写");
            }
            if (TextUtils.isEmpty(deviceData.sbzj)){
                throw new ValidateException("设置折旧尚未填写");
            }
            if (TextUtils.isEmpty(deviceData.sbhd)){
                throw new ValidateException("设置能耗尚未填写");
            }
        }
        return mDeviceDataList;
    }

    public void setResult(List<DeviceData> deviceDataList) {
        mDeviceDataList = deviceDataList;
        notifyDataSetChanged();
    }

    public void add() {
        mDeviceDataList.add(new DeviceData());
        notifyItemInserted(mDeviceDataList.size()+1);
    }
    public void delete(int position) {
        mDeviceDataList.remove(position);
        int real=position+1;
        notifyItemRemoved(real);
//        if(real != mDeviceDataList.size()+1){ // 如果移除的是最后一个，忽略
//            notifyItemRangeChanged(position, mDeviceDataList.size() - real);
//        }
    }
    public DeviceDataAdapter(AddAndDeleteCallBack andDeleteCallBack) {
        mAndDeleteCallBack = andDeleteCallBack;
    }

    private static final int ADD = 0;
    private static final int NORMAL = 1;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ADD) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_simulation_device_add, parent, false);
            return new HeadViewHolder(view);
        } else {
            View view1 = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_simulation_device_data, parent, false);
            return new HViewHolder(view1, new MyCustomEditTextListener());
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HViewHolder) {
            int realposition=holder.getAdapterPosition()-1;
            DeviceData data=mDeviceDataList.get(realposition);
            HViewHolder hViewHolder = (HViewHolder) holder;
            hViewHolder.deviceName.setText(data.machineName);
            hViewHolder.zjf.setText(data.sbzj);
            hViewHolder.nhf.setText(data.sbhd);
            hViewHolder.deviceName.setEnabled(editenable);
            hViewHolder.zjf.setEnabled(editenable);
            hViewHolder.nhf.setEnabled(editenable);
            hViewHolder.deleteSimDevice.setEnabled(editenable);
            hViewHolder.deleteSimDevice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mAndDeleteCallBack != null) {
                        mAndDeleteCallBack.delete(realposition);
                    }
                }
            });
            hViewHolder.deviceName.addTextChangedListener(new CustomTextWatch() {
                @Override
                public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                    if (realposition>=mDeviceDataList.size()){
                        mDeviceDataList.get(mDeviceDataList.size()-1).machineName=charSequence.toString();
                    }else {
                        mDeviceDataList.get(realposition).machineName=charSequence.toString();
                    }
                }
            });
            hViewHolder.nhf.addTextChangedListener(new CustomTextWatch() {
                @Override
                public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                    if (realposition>=mDeviceDataList.size()){
                        mDeviceDataList.get(mDeviceDataList.size()-1).sbhd=charSequence.toString();
                    }else {
                        mDeviceDataList.get(realposition).sbhd=charSequence.toString();
                    }
                }
            });
            hViewHolder.zjf.addTextChangedListener(new CustomTextWatch() {
                @Override
                public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                    if (realposition>=mDeviceDataList.size()){
                        mDeviceDataList.get(mDeviceDataList.size()-1).sbzj=charSequence.toString();
                    }else {
                        mDeviceDataList.get(realposition).sbzj=charSequence.toString();
                    }
                }
            });
        }
        if (holder instanceof HeadViewHolder) {
            HeadViewHolder headViewHolder = (HeadViewHolder) holder;
            headViewHolder.addSimDevice.setEnabled(editenable);
            headViewHolder.addSimDevice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mAndDeleteCallBack != null) {
                        mAndDeleteCallBack.add();
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDeviceDataList!=null?mDeviceDataList.size()+1:0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ADD;
        } else {
            return NORMAL;
        }
    }

    class HViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.device_name)
        EditText deviceName;
        @BindView(R.id.zjf)
        EditText zjf;
        @BindView(R.id.nhf)
        EditText nhf;
        @BindView(R.id.delete_sim_device)
        ImageView deleteSimDevice;
        MyCustomEditTextListener mEditTextListener;

        public HViewHolder(View itemView, MyCustomEditTextListener mEditTextListener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mEditTextListener = mEditTextListener;
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.add_sim_device)
        ImageView addSimDevice;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    private class MyCustomEditTextListener implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
//            mSimulationList.get(position).value = charSequence.toString();
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }

    public interface AddAndDeleteCallBack {
        void add();

        void delete(int position);
    }
}